<template>
  <div @click="$router.push('/article/'+artId)">
    <!-- 文章的信息 -->
    <van-cell>
      <template #title>

        <div class="title-box">
          <span>{{article.title}}</span>
          <img v-lazy="article.cover.images[0]" alt="" class="thumb" v-if="article.cover.type===1" />
        </div>
        <div class="thumb-box" v-if="article.cover.type===3">
          <img v-for="(item,index) in article.cover.images" :key="index" v-lazy="item" alt="" class="thumb">
        </div>
      </template>

      <template #label>
        <div class="lable-box">
          <span>{{article.aut_name}} &nbsp;&nbsp; {{article.comm_count}}评论 &nbsp;&nbsp; {{article.pubdate |dateFormat}}</span>
          <!-- 关闭按钮 -->
          <!-- 通过 .stop 事件修饰符，阻止点击事件的冒泡 -->
          <van-icon name="cross" @click.stop='show=true' v-if="closable" />
        </div>
      </template>

    </van-cell>

    <!-- 反馈的动作面板 -->
    <van-action-sheet v-model='show' cancel-text="取消" :closeable='false' @closed='isFirst=true' get-container="body">
      <!-- 第一级反馈面板 -->
      <div v-if="isFirst">
        <van-cell v-for="(item,index) in actions" :key="index" :title="item.name" clickable class="center-title" @click="onCellClick(item.name)" />
      </div>
      <!-- 第二级反馈面板 -->
      <div v-else>
        <van-cell title="返回" clickable title-class="center-title" @click="isFirst=true" />
        <van-cell v-for="item in reports" :key="item.type" clickable title-class="center-title" :title="item.label" @click="reportArticleAPI(item.type)" />
      </div>
    </van-action-sheet>

  </div>
</template>

<script>
import reports from '@/api/reports'
import { dislikeArticleAPI, reportArticleAPI } from '@/api/homeAPI.js'
export default {
  name: 'ArtItem',
  props: {
    article: {
      type: Object,
      require: true
    },
    closable: {
      type: Boolean,
      // 默认值为 true，表示展示关闭按钮
      default: true
    }
  },
  data() {
    return {
      show: false,
      actions: [
        { name: '不感兴趣' },
        { name: '拉黑作者' },
        { name: '反馈垃圾内容' }
      ],
      isFirst: true,
      reports
    }
  },
  methods: {
    async onCellClick(name) {
      if (name === '不感兴趣') {
        console.log('不感兴趣')
        const { data: res } = await dislikeArticleAPI(this.artId)
        if (res.message === 'OK') {
          console.log(res)
          this.$emit('remove-article', this.artId)
        }
        this.show = false
      } else if (name === '拉黑作者') {
        console.log('拉黑作者')
        this.show = false
      } else if (name === '反馈垃圾内容') {
        // TODO：展示二级反馈面板
        this.isFirst = false
      }
    },
    async reportArticleAPI(type) {
      const { data: res } = await reportArticleAPI(this.artId, type)
      if (res.message === 'OK') {
        this.$emit('remove-article', this.artId)
      }
      this.show = false
    }
  },
  computed: {
    artId() {
      return this.article.art_id.toString()
    }
  }
}
</script>

<style lang='less' scoped>
.lable-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.thumb-box {
  display: flex;
  justify-content: space-between;
}
.center-title {
  text-align: center;
}
</style>
